package com.cq.androidtools.flow

import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

/**
 * 作者: CQ
 * 日期: 2022-06-10
 * 说明:
 */
suspend fun simple1() : List<Int> {
    delay(500)
    return listOf(0, 1, 2)
}

fun simple2() : Flow<Int> = flow {
    for (i in 1..3) {
        delay(100)
        emit(i)
    }
}

fun main() = runBlocking {
//    simple1().forEach { println(it) }
    // 启动并发的协程以验证主线程并未阻塞
    launch {
        for (k in 1..3) {
            println("I'm not blocked $k")
            delay(100)
        }
    }
    // 收集这个流
    simple2().collect { value -> println(value) }
}

